/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 23735
 * Date: 2023-10-01
 * Time: 20:39
 */
public class Solution {

    /**
     *  二分查找
     */
    public int smallestDivisor(int[] nums, int threshold) {
        int len = nums.length;
        int left = 1;
        int right = 1000000;
        while (left < right) {
            int mid = ((right-left)>>1) + left;
            if (!judge(nums, mid, threshold)) {
                left = mid + 1;
            } else {
                right = mid;
            }
        }
        return left;
    }

    private boolean judge(int[] nums, int mid, int threshold) {
        int sum = 0;
        for (int x: nums) {
            sum += (x+mid-1) / mid;
        }
        return sum <= threshold;
    }
}
